/* 3.6 Топологическая сортировка */

#include <stdio.h> 

struct edge
{
	int x, y;
};

int dfs(int p, int* v, int* r, int* k, struct edge* edges, int e);//return 1 if impossible

int main()
{
	int n = 7, e = 7;
	struct edge edges[10] = {{0, 2}, {6, 5}, {1, 4}, {2, 1}, 
			{3, 4}, {2, 3}, {3, 1}};
	/*int n = 14, e = 10;
	struct edge edges[10] = {{6, 8}, {6, 0}, {10, 0}, {0, 3}, {13, 2}, 
				{12, 11}, {11, 1}, {5, 1}, {5, 9}, {12, 5}};*/
	int v[20] = {0};//visited 1-in 2-out
	int r[20] = {0};//topsorted verticles
	int i = 0;
	int k = 0;
	for (i = 0; i < n; i++)
	{
		if (v[i] != 2)
		if (dfs(i, v, r, &k, edges, e)) 
		{
			printf("Impossible\n");
			return 0;
		}
	}
	for (i = n-1; i >= 0; i--)
	{
		printf("%d ", r[i]+1);
	}
	printf("\n");
	return 0;
}

int dfs(int p, int* v, int* r, int* k, struct edge* edges, int e)
{
	int i = 0;
	if (v[p] == 1) return 1;
	v[p] = 1;
	for (i = 0; i < e; i++)
		if (edges[i].x == p && v[edges[i].y] != 2)
		{ 
			if (dfs(edges[i].y, v, r, k, edges, e)) return 1;
		}
	r[(*k)++] = p;
	v[p] = 2;
	return 0;
}

